<?php
namespace addons\TfFutures\backend\controllers;

use common\helpers\ArrayHelper;
use common\models\sys\Manager;
use Yii;
use yii\data\Pagination;
use addons\TfFutures\common\models\Logs;


class LogsController extends BaseController
{
    public $modelClass = Logs::class;
    /**
     * 首页
     *
     * @return string
     */
    public function actionIndex()
    {
        $search = Yii::$app->request->get('keyword','');
        $type=Yii::$app->request->get('close_type','');

        $data   = $this->modelClass::find()->joinWith('member');
        if (!empty($search)) {
            $data->andFilterWhere(['or',['like','rf_member.username',$search],['like','rf_member.mobile',$search]]);
        }
        if (!empty($type)) {
            $data->andFilterWhere(['like','rf_addon_tf_futures_logs.type',$type]);
        }
        $daili = Manager::find()->select("rf_sys_manager.id,rf_sys_manager.username")->join('LEFT JOIN','rf_common_auth_assignment','rf_sys_manager.id=rf_common_auth_assignment.user_id')->where(["rf_common_auth_assignment.role_id"=>4,"rf_sys_manager.status"=>1])->asArray()->all();
        $daili = ArrayHelper::map($daili,"username","id");
        $id = Yii::$app->user->id;  //当前登录id
        if(!empty(in_array($id,$daili))) {
            //查询此代理下面的用户订单
            $member = \common\models\member\Member::find()->where(['daili'=>$id])->select('id,username')->asArray()->all();
            foreach ($member as $item){
                //获取到用户的id
                $member_id[] = $item['id'];
            }
            if(!empty($member_id)){
                $data->andFilterWhere(['in','member_id',$member_id]);
            } else {
                $data->andFilterWhere(['=','member_id',-1]);
            }
        }
        $pages  = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
        $models = $data->offset($pages->offset)->orderBy('created_at desc')->limit($pages->limit)->all();
        return $this->render($this->action->id, [
            'models' => $models,
            'pages' => $pages,
            'keyword'=>$search,

        ]);
    }
}